/**
 * Copyright (C) 2009, 2010 SC 4ViewSoft SRL
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ss.competition.chart;

import java.util.Vector;

import org.achartengine.ChartFactory;
import org.achartengine.renderer.DefaultRenderer;

import Datas.Data;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;

import com.ss.competition.operators.Query;

/**
 * Budget demo pie chart.
 */
public class BudgetPieChart extends AbstractChart {
 
	
	int[] Colors = new int[]
	 {Color.BLUE, Color.GREEN, Color.MAGENTA, Color.YELLOW, Color.CYAN,Color.LTGRAY, Color.GRAY, Color.DKGRAY, Color.TRANSPARENT, Color.RED};
	/**
   * Returns the chart name.
   * @return the chart name
   */
  public String getName() {
    return "Budget chart";
  }
  
  /**
   * Returns the chart description.
   * @return the chart description
   */
  public String getDesc() {
    return "The budget per project for this year (pie chart)";
  }
  
  /**
   * Executes the chart demo.
   * @param context the context
   * @return the built intent
   */
  public Intent execute(Context context, Query queryManeger) {
	
	Vector<Data> expense = queryManeger.queryAll();
	
	Vector<Data> result =  new Vector<Data>() ;
	Vector<Data> tmp = new Vector<Data>();
	 int i,j;
	 
	 for (i = 0; i < expense.size(); i++)
	 {
		 if (expense.get(i).expense > 0)
			 result.add(expense.get(i));
	 }
	 
    for ( i = 0; i < result.size(); i++)
    {
    	for ( j = 0; j < tmp.size(); j++)
    	{
    		if (result.get(i).purpose.equals(tmp.get(j).purpose))
    		{
    		    tmp.get(j).expense += result.get(i).expense;
    		    break;
    		}  			
    	}	
    	if (j == tmp.size())
    		tmp.add(result.get(i));
    	
    }

 //  Log.v("hahaha", "exp ="+expense.size());
   
 //   Log.v("hahaha", "tmp ="+tmp.size());
    	
    double[] values = new double[tmp.size()];
    int[] colors = new int[tmp.size()];// {Color.BLUE, Color.GREEN, Color.MAGENTA, Color.YELLOW, Color.CYAN,Color.LTGRAY};
   
    for ( i = 0; i < tmp.size(); i++)
    {
    	values[i] = result.get(i).expense;
    	colors[i] = Colors[i];
    }
	  //double[] values = new double[] {12, 14, 11, 10, 19,100};
	    
   
    DefaultRenderer renderer = buildCategoryRenderer(colors);
    renderer.setLabelsTextSize(10);
    return ChartFactory.getPieChartIntent(context, buildCategoryDataset("123 budget", values, tmp), renderer);
  }

}
